package com.mxpio.erp.workshop.service.impl;

import com.mxpio.erp.common.inventory.entity.WarehouseOrderLine;
import com.mxpio.erp.common.workshop.entity.*;
import com.mxpio.erp.common.workshop.service.OODefectiveProductsNoticeService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.JpaUtil;
import com.mxpioframework.system.service.impl.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class OODefectiveProductsNoticeServiceImpl extends BaseServiceImpl<OODefectiveProductsNotice> implements OODefectiveProductsNoticeService {
    @Override
    @Transactional
    public Result<List<OODefectiveProductsNotice>> listByIds(String noticeNos) {
        if (StringUtils.isBlank(noticeNos)){
            return Result.error("查询参数为空");
        }
        String[] split = noticeNos.split(",");
        List<String> noticeNoList = Stream.of(split).collect(Collectors.toList());
        List<OODefectiveProductsNotice> list = JpaUtil.linq(OODefectiveProductsNotice.class)
                .collect("noticeNo", OODefectiveProductsNoticeLine.class, "noticeNo")
                .collect("bizNo", OutsourceOrder.class,"bizOrderNo")
                .in("noticeNo", noticeNoList).list();

        if (list.size()>0){
            //关联上游订单行中的下料长度、可制数量等字段
            Set<String> bizNoSet = JpaUtil.collect(list, "bizOrderNo");
            List<OutsourceOrderItem> orderItems = JpaUtil.linq(OutsourceOrderItem.class).in("bizNo", bizNoSet).list();
            Map<String, List<OutsourceOrderItem>> orderItemsMap = orderItems.stream().collect(Collectors.groupingBy(OutsourceOrderItem::getBizNo));
            for (OODefectiveProductsNotice notice:list){
                List<OODefectiveProductsNoticeLine> noticeLines = notice.getOoDefectiveProductsNoticeLines();
                String bizNo = notice.getBizOrderNo();
                if (orderItemsMap.containsKey(bizNo)){
                    List<OutsourceOrderItem> orderItemsPerOo = orderItemsMap.get(bizNo);
                    if (orderItemsPerOo!=null&&orderItemsPerOo.size()>0){
                        Map<Object, OutsourceOrderItem> lineNoMap = JpaUtil.index(orderItemsPerOo, "lineNo");
                        for (OODefectiveProductsNoticeLine noticeLine:noticeLines){
                            String originBizLineNo = noticeLine.getOriginBizLineNo();
                            if (lineNoMap.containsKey(originBizLineNo)&&lineNoMap.get(originBizLineNo)!=null){
                                OutsourceOrderItem orderItem = lineNoMap.get(originBizLineNo);

                                noticeLine.setMaterialBrand(orderItem.getMaterialBrand());
                                noticeLine.setMakeLength(orderItem.getMakeLength());
                                noticeLine.setMaterialType(orderItem.getMaterialType());
                                noticeLine.setMakeArea(orderItem.getMakeArea());
                                noticeLine.setMakeNum(orderItem.getMakeNum());
                            }
                        }
                    }
                }
            }
            //关联下游仓单行中对应的数量
            List<WarehouseOrderLine> wolines = JpaUtil.linq(WarehouseOrderLine.class).in("originNoticeNo", noticeNoList).list();
            if (wolines.size()>0){//关联生成的仓单明细数量，即为通知单明细对应的确认数量
                Map<String, List<WarehouseOrderLine>> wolinesMap = wolines.stream().collect(Collectors.groupingBy(WarehouseOrderLine::getOriginNoticeNo));
                for (OODefectiveProductsNotice notice:list){
                    List<OODefectiveProductsNoticeLine> noticeLines = notice.getOoDefectiveProductsNoticeLines();
                    String noticeNo = notice.getNoticeNo();
                    if (wolinesMap.containsKey(noticeNo)){
                        List<WarehouseOrderLine> wolinesPerNo = wolinesMap.get(noticeNo);
                        if (wolinesPerNo!=null&&wolinesPerNo.size()>0){
                            Map<String, WarehouseOrderLine> wolinesPerNoMap = JpaUtil.index(wolinesPerNo, "originNoticeLineNo");
                            for (OODefectiveProductsNoticeLine noticeLine:noticeLines){
                                String lineNo = noticeLine.getLineNo();
                                if (wolinesPerNoMap.containsKey(lineNo)&&wolinesPerNoMap.get(lineNo)!=null){
                                    WarehouseOrderLine warehouseOrderLine = wolinesPerNoMap.get(lineNo);
                                    noticeLine.setWoLineQuantity(warehouseOrderLine.getQuantity());//关联确认数量
                                }
                            }
                        }
                    }
                }
            }
        }

        return Result.OK(list);
    }
}
